﻿using System.Globalization;

namespace CodeContrib
{
	public static class NumberFormatInfoExtensions
	{
		public static string GetCurrencyNegativePattern(this NumberFormatInfo numberFormatInfo)
		{
			return GetCurrencyNegativePattern(numberFormatInfo, "$", "n");
		}

		public static string GetCurrencyNegativePattern(this NumberFormatInfo numberFormatInfo, string symbolFormat, string numberFormat)
		{
			var format = string.Empty;

			switch (numberFormatInfo.CurrencyNegativePattern)
			{
				case 0:
					{
						format = "({0}{1})";

						break;
					}
				case 1:
					{
						format = "-{0}{1}";

						break;
					}
				case 2:
					{
						format = "{0}-{1}";

						break;
					}
				case 3:
					{
						format = "{0}{1}-";

						break;
					}
				case 4:
					{
						format = "({1}{0})";

						break;
					}
				case 5:
					{
						format = "-{1}{0}";

						break;
					}
				case 6:
					{
						format = "{1}-{0}";

						break;
					}
				case 7:
					{
						format = "{1}{0}-";

						break;
					}
				case 8:
					{
						format = "-{1} {0}";

						break;
					}
				case 9:
					{
						format = "-{0} {1}";

						break;
					}
				case 10:
					{
						format = "{1} {0}-";

						break;
					}
				case 11:
					{
						format = "{0} {1}-";

						break;
					}
				case 12:
					{
						format = "{0} -{1}";

						break;
					}
				case 13:
					{
						format = "{1}- {0}";

						break;
					}
				case 14:
					{
						format = "({0} {1})";

						break;
					}
				case 15:
					{
						format = "({1} {0})";

						break;
					}
			}

			return string.Format(format, symbolFormat, numberFormat);
		}

		public static string GetCurrencyPositivePattern(this NumberFormatInfo numberFormatInfo)
		{
			return GetCurrencyPositivePattern(numberFormatInfo, "$", "n");
		}

		public static string GetCurrencyPositivePattern(this NumberFormatInfo numberFormatInfo, string symbolFormat, string numberFormat)
		{
			var format = string.Empty;

			switch (numberFormatInfo.CurrencyPositivePattern)
			{
				case 0:
					{
						format = "{0}{1}";

						break;
					}
				case 1:
					{
						format = "{1}{0}";

						break;
					}
				case 2:
					{
						format = "{0} {1}";

						break;
					}
				case 3:
					{
						format = "{1} {0}";

						break;
					}
			}

			return string.Format(format, symbolFormat, numberFormat);
		}

		public static string GetNumberNegativePattern(this NumberFormatInfo numberFormatInfo)
		{
			return GetNumberNegativePattern(numberFormatInfo, "n");
		}

		public static string GetNumberNegativePattern(this NumberFormatInfo numberFormatInfo, string numberFormat)
		{
			var format = "({0})";

			switch (numberFormatInfo.NumberNegativePattern)
			{
				case 0:
					{
						format = "({0})";

						break;
					}
				case 1:
					{
						format = "-{0}";

						break;
					}
				case 2:
					{
						format = "- {0}";

						break;
					}
				case 3:
					{
						format = "{0}-";

						break;
					}
				case 4:
					{
						format = "{0} -";

						break;
					}
			}

			return string.Format(format, numberFormat);
		}

		public static string GetPercentNegativePattern(this NumberFormatInfo numberFormatInfo)
		{
			return GetPercentNegativePattern(numberFormatInfo, "n", "%");
		}

		public static string GetPercentNegativePattern(this NumberFormatInfo numberFormatInfo, string numberFormat, string symbolFormat)
		{
			var format = "-{0} {1}";

			switch (numberFormatInfo.PercentNegativePattern)
			{
				case 0:
					{
						format = "-{0} {1}";

						break;
					}
				case 1:
					{
						format = "-{0}{1}";

						break;
					}
				case 2:
					{
						format = "-{1}{0}";

						break;
					}
				case 3:
					{
						format = "{1}-{0}";

						break;
					}
				case 4:
					{
						format = "{1}{0}-";

						break;
					}
				case 5:
					{
						format = "{0}-{1}";

						break;
					}
				case 6:
					{
						format = "{0}{1}-";

						break;
					}
				case 7:
					{
						format = "-{1} {0}";

						break;
					}
				case 8:
					{
						format = "{0} {1}-";

						break;
					}
				case 9:
					{
						format = "{1} {0}-";

						break;
					}
				case 10:
					{
						format = "{1} -{0}";

						break;
					}
				case 11:
					{
						format = "{0}- {1}";

						break;
					}
			}

			return string.Format(format, numberFormat, symbolFormat);
		}

		public static string GetPercentPositivePattern(this NumberFormatInfo numberFormatInfo)
		{
			return GetPercentPositivePattern(numberFormatInfo, "n", "%");
		}

		public static string GetPercentPositivePattern(this NumberFormatInfo numberFormatInfo, string numberFormat, string symbolFormat)
		{
			var format = "{0} {1}";

			switch (numberFormatInfo.PercentPositivePattern)
			{
				case 0:
					{
						format = "{0} {1}";

						break;
					}
				case 1:
					{
						format = "{0}{1}";

						break;
					}
				case 2:
					{
						format = "{1}{0}";

						break;
					}
				case 3:
					{
						format = "{1} {0}";

						break;
					}
			}

			return string.Format(format, numberFormat, symbolFormat);
		}

		public static bool HasCurrencySymbol(this NumberFormatInfo numberFormatInfo)
		{
			return (!string.IsNullOrEmpty(numberFormatInfo.CurrencySymbol));
		}

		public static bool HasPercentSymbol(this NumberFormatInfo numberFormatInfo)
		{
			return (!string.IsNullOrEmpty(numberFormatInfo.PercentSymbol));
		}

		public static bool IsPercentBeforeNumber(this NumberFormatInfo numberFormatInfo)
		{
			return (GetPercentPositivePattern(numberFormatInfo).IndexOf("%") < GetPercentPositivePattern(numberFormatInfo).IndexOf("n"));
		}

		public static bool IsSymbolBeforeNumber(this NumberFormatInfo numberFormatInfo)
		{
			return (GetCurrencyPositivePattern(numberFormatInfo).IndexOf("$") < GetCurrencyPositivePattern(numberFormatInfo).IndexOf("n"));
		}
	}
}